<?php

namespace app\process;

use app\model\Device;
use Workerman\Crontab\Crontab;

class CheckHeart
{
    public function onWorkerStart()
    {
        // 每55秒执行一次
        new Crontab('*/60 * * * * *', function () {
            // 针对开机的监测心跳
            $devices =  Device::query()->get();

            foreach ($devices as $device) {
                // var_dump($device['last_send_at']);
                if ((time() - $device['last_send_at']) > ini('CHECK_HEART_SECONDS', 60)) {
                    //如果超过60秒没消息就设置离线
                    $device['is_online'] = 0;
                    $device['status'] = 0;
                }else{
                    $device['is_online'] = 1;
                }

                $device->save();
            }
        });
    }
}
